package commands;

import utils.CommandStack;
import utils.EditorContext;
import utils.FileManager;

public class SaveCommand extends Command {
    private String filePath;

    public SaveCommand(EditorContext context, String filePath) {
        super(context);
        this.filePath = filePath;
    }

    @Override
    public void execute() {
        context.setSavingState(EditorContext.SavingState.Saved);
        FileManager.saveFile(filePath, context.getContent());
        // Reset context after saving.
        context.setContent("");
        context.setCurrentFilePath(null);
        context.setState(EditorContext.EditorState.IDLE);
        CommandStack.getInstance().add(new SaveCommand(
                new EditorContext(new StringBuilder(context.getContent())), filePath));
    }

}

